package com.aier.user.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.user.entity.Dialog;

public class DialogRowMapper implements RowMapper<Dialog> {

    @SuppressWarnings("unused")
    private int type;

    public DialogRowMapper() {
    }

    public DialogRowMapper(int type) {
        this.type = type;
    }

    @Override
    public Dialog mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseHome(new Dialog(), rs);
    }

    public static Dialog parseHome(Dialog h, ResultSet rs) throws SQLException {
        return parseHome(h, rs, null);
    }

    public static Dialog parseHome(Dialog h, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            h.setDid(rs.getLong(su+"did"));
        } catch (SQLException e) {
            h.setDid(null);
        }
        try {
            h.setTid(rs.getLong(su+"tid"));
        } catch (SQLException e) {
            h.setTid(null);
        }
        try {
            h.setPuid(rs.getLong(su+"puid"));
        } catch (SQLException e) {
            h.setPuid(null);
        }
        try {
            h.setBuid(rs.getLong(su+"buid"));
        } catch (SQLException e) {
            h.setBuid(null);
        }
        try {
            h.setCid(rs.getLong(su+"cid"));
        } catch (SQLException e) {
            h.setCid(null);
        }
        try {
            h.setType(rs.getShort(su+"type"));
        } catch (SQLException e) {
            h.setType(null);
        }
        try {
            String str = rs.getString(su+"content");
            if (str != null) {
                str = str.replaceAll("<", "&lt;");
                str = str.replaceAll(">", "&gt;");
            }
            h.setContent(str);
        } catch (SQLException e) {
            h.setContent(null);
        }
        try {
            h.setCdate(rs.getTimestamp(su+"cdate"));
        } catch (SQLException e) {
            h.setCdate(null);
        }
        try {
            h.setTname(rs.getString(su+"tname"));
        } catch (SQLException e) {
            h.setTname(null);
        }
        try {
            h.setBname(rs.getString(su+"bname"));
        } catch (SQLException e) {
            h.setBname(null);
        }
        try {
            h.setHeadimg(rs.getString(su+"headimg"));
        } catch (SQLException e) {
            h.setHeadimg(null);
        }
        try {
            h.setRelation(rs.getString(su+"relation"));
        } catch (SQLException e) {
            h.setRelation(null);
        }
        return h;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, Dialog h) {
        par.put("did",h.getDid());
        par.put("tid",h.getTid());
        par.put("puid",h.getPuid());
        par.put("buid",h.getBuid());
        par.put("cid",h.getCid());
        par.put("type",h.getType());
        par.put("content",h.getContent());
        par.put("cdate",h.getCdate());
        par.put("tname",h.getTname());
        par.put("bname",h.getBname());
        par.put("headimg",h.getHeadimg());
        par.put("relation",h.getRelation());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, Dialog h) {
        args.put("did",h.getDid() );
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final Dialog h) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(h),
                        new String[] { "did" });
                parsePS(ps, h);
                return ps;
            }

        };
    }

    private static String parseSQL(Dialog h) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into dialog(");
        int count = 0;
        if (null != h.getTid()) {
            sql.append("tid,");
            count++;
        }
        if (null != h.getPuid()) {
            sql.append("puid,");
            count++;
        }
        if (null != h.getBuid()) {
            sql.append("buid,");
            count++;
        }
        
        if (null != h.getCid()) {
            sql.append("cid,");
            count++;
        }
        if (null != h.getHeadimg()) {
            sql.append("headimg,");
            count++;
        }
        if (null != h.getType()) {
            sql.append("type,");
            count++;
        }
        if (null != h.getContent()) {
            sql.append("content,");
            count++;
        }
        if (null != h.getCdate()) {
            sql.append("cdate,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, Dialog h)
        throws SQLException {
        int count = 1;
        if (null != h.getTid()) {
            ps.setLong(count++,h.getTid());
        }
        if (null != h.getPuid()) {
            ps.setLong(count++,h.getPuid());
        }
        if (null != h.getBuid()) {
            ps.setLong(count++,h.getBuid());
        }
        if (null != h.getCid()) {
            ps.setLong(count++,h.getCid());
        }
        if (null != h.getHeadimg()) {
            ps.setString(count++,h.getHeadimg());
        }
        if (null != h.getType()) {
            ps.setLong(count++,h.getType());
        }
        if (null != h.getContent()) {
            ps.setString(count++,h.getContent() );
        }
        if (null != h.getCdate()) {
            ps.setTimestamp(count++,h.getCdate());
        }
    }

}
